package com.doyens.item.controller;

import com.doyens.common.pojo.PageResult;
import com.doyens.item.dto.SpuDTO;
import com.doyens.item.pojo.Sku;
import com.doyens.item.pojo.Spu;
import com.doyens.item.pojo.SpuDetail;
import com.doyens.item.service.GoodsService;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品spu列表
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){
        PageResult<SpuDTO> result = this.goodsService.querySpuByPage(key,saleable,page,rows);
        if (result ==null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);

    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO ){
        this.goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //回显商品详情
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail>  querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){
        SpuDetail spuDetail =  this.goodsService.querySpuDataiBySpuId(spuId);
        if(spuDetail ==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);

    }

    //显示sku列表
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long id){
       List<Sku>  skus = this.goodsService.querySkusBySpuId(id);
       if (CollectionUtils.isEmpty(skus)){
           return ResponseEntity.notFound().build();
       }
       return ResponseEntity.ok(skus);
    }

    //修改商品信息
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        this.goodsService.updateGoods(spuDTO);
        return ResponseEntity.noContent().build();
    }

    //根据spuId查询spu表中的数据
    @GetMapping("{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        Spu spu = this.goodsService.querySpuById(id);
        if (spu == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spu);
    }

    @GetMapping("sku/{skuId}")
    public ResponseEntity<Sku> queryBySkuId(@PathVariable("skuId") Long skuId){
        Sku sku =  this.goodsService.queryBySkuId(skuId);
        if ( sku == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }

}
